package cool.mtc.minghe.web.controller.sys.menu;

import cool.mtc.core.page.PageData;
import cool.mtc.minghe.data.model.base.IdForm;
import cool.mtc.minghe.data.model.sys.menu.dto.MenuDTO;
import cool.mtc.minghe.data.model.sys.menu.dto.MenuDictDTO;
import cool.mtc.minghe.data.model.sys.menu.dto.MenuListDTO;
import cool.mtc.minghe.data.model.sys.menu.form.MenuForm;
import cool.mtc.minghe.data.model.sys.menu.query.MenuQuery;
import cool.mtc.minghe.service.service.sys.menu.MenuService;
import cool.mtc.minghe.web.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 明河
 */
@Tag(name = "菜单")
@RestController
@RequestMapping("api/sys/menu")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MenuController extends BaseController {
    private final MenuService menuService;

    @Tag(name = "菜单")
    @Operation(summary = "新增")
    @PostMapping("insert")
    public long insert(@RequestBody @Validated MenuForm form) {
        return menuService.insert(form);
    }

    @Tag(name = "菜单")
    @Operation(summary = "更新")
    @PostMapping("update")
    public void update(@RequestBody @Validated MenuForm form) {
        menuService.update(form);
    }

    @Tag(name = "菜单")
    @Operation(summary = "删除")
    @PostMapping("delete")
    public void delete(@RequestBody @Validated IdForm form) {
        menuService.delete(form.getId());
    }

    @Tag(name = "菜单")
    @Operation(summary = "分页列表")
    @GetMapping("list/page")
    public PageData<MenuListDTO> pageList(MenuQuery query) {
        return menuService.pageList(query);
    }

    @Tag(name = "菜单")
    @Operation(summary = "详情")
    @GetMapping("info")
    public MenuDTO info(Long id) {
        return menuService.info(id);
    }

    @Tag(name = "菜单")
    @Operation(summary = "列表")
    @GetMapping("list")
    public List<MenuListDTO> list(MenuQuery query) {
        return menuService.list(query);
    }

    @PreAuthorize("@ps.hasPermission('sys:menu:read')")
    @Tag(name = "菜单")
    @Operation(summary = "字典列表")
    @GetMapping("list/dict")
    public List<MenuDictDTO> dictList() {
        return menuService.dictList();
    }

    @PreAuthorize("@ps.hasPermission('sys:menu:read')")
    @Tag(name = "菜单")
    @Operation(summary = "当前用户的菜单列表")
    @GetMapping("list/current")
    public List<MenuListDTO> currentList() {
        return menuService.currentList();
    }
}
